<?php
class ArticleAction extends CommAction {
	var $url = "__APP__/Article";
	function index(){
		$c_id = $_GET['cid'];
		$article_id = $_GET['aid'];
		
		//$this->assign('cato_output',$this->tree);	
		$url = "Article:article_list";
		
		$article = d("article");
		import("ORG.Util.Page");
		
		$count = $article->count();
		$p  = new Page ($count, 10 ); 
		
		$p->setConfig('header','篇记录');
        $p->setConfig('prev',"<");
        $p->setConfig('next','>');
        $p->setConfig('first','<<');
        $p->setConfig('last','>>');
            
		if(!empty($c_id)){
			$condition = "C_id = ".$_GET['cid'];
		}
		elseif(!empty($article_id)){
			
			$condition = "id = '".$article_id."'";
			$url = "Article:article_content";
			$this->assign ( "nowp", $_GET['nowp']);
		}
		else{
			$condition = '1';
		}
		//echo ($condition);
		
		$list = $article->where($condition)->order('ctime desc')->limit($p->firstRow.','.$p->listRows)->findAll(); 
		//dump($list);

		$page = $p->show (); 
		$this->assign ( "c_id", $c_id );
		$this->assign ( "page", $page );
        $this->assign ( "list", $list );
        $this->assign ("current_p",$p->nowPage);
        

		$this->display($url);
	}

	
	function make_ArticleId($time,$c_id){
		$n1 = date("YmdHis",$time);
		$n2 = intval(rand(1,100));		
		return $id=$c_id.$n1.$n2;
	
	}
	
	function article_add(){
		
		if($_POST['article_bt']){
			$article_arr['cid'] = "2";
			$article_arr['title'] = $_POST['article__title'];
			$article_arr['content'] = stripslashes($_POST['article_content']);
			$article_arr['ctime'] = time();
			$article_arr['id'] = $this->make_ArticleId($article_arr['ctime'],$article_arr['cid']);
			
			if(d("article")->add($article_arr)){
				//echo d("article")->getlastsql();
				$this->assign('jumpUrl',"__APP__/article/index/nid/".$article_arr['id']); 
				$this->success("添加新闻成功!");
			}

			
		}
		else{
			$this->assign('cato_output',$this->tree);	
			//dump($news);
			$this->display("Article:article_add");
		}
	}
	
	function article_del(){
		//echo $_POST['news_row'];
		$article_id = implode("','",$_POST['article_row']);
		//echo $news_id;
		
		//echo "$article_id in ('".$article_id."')";
		if($article_id){
			if(d('article')->where("id in ('".$article_id."')")->delete()){
				$msg = "删除新闻成功!";
			}
			else{
				$msg = "删除新闻失败!";
			}
		}
		else{
			$msg = "news_id不能为空";
		}
		
		$this->assign('jumpUrl',$this->url); 
		$this->success($msg);
	}
	
		
	function article_edit(){

		$this->assign ( "nowp", $_GET['nowp']);

		if($_POST['article_bt']){
			$article_arr['id'] = $_POST['aid'];
			$article_arr['title'] = $_POST['article__title'];
			$article_arr['content'] = stripslashes($_POST['article_content']);
			$article_arr['cid'] = '2';
			$article_arr['ctime'] = time();
			$condition = "id = '".$article_arr['id']."'";
			
			if(d("article")->where($condition)->save($article_arr)){
				$msg = "新闻修改成功!";
				$url = "__APP__/article/index/aid/".$article_arr['id']."/nowp/".$_GET['nowp'];
				
			}
			else{
				$msg = "新闻修改失败!";
				$url = "__APP__/article/article_edit/aid/".$article_arr['id'];
			}
			//$this->news_make($new_arr['News_id']);
			//echo $url;
			$this->assign('jumpUrl',$url); 
		    $this->success($msg);		
		}
		else{
			$article_id = $_GET['aid'];
			if($article_id){
				$this->assign('article', d("article")->where("id = '".$article_id."'")->find());
			}
		
			//dump($news);
			$this->assign('cato_output',$this->tree);
			$this->display("Article:article_add");
		}
	}
	
	
	
}

?>